const { Sequelize, DataTypes } = require('sequelize');
const sequelize = new Sequelize('nodejs1', 'sa', '123456', {
    host: 'localhost',
    dialect: 'mssql'
})
let Login = sequelize.define('logins',
    {
        zhanghao: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false
        }
    }

)
let Class = sequelize.define('class', {
    CName: {
        type: DataTypes.STRING,
        allowNull: false
    },
    Sex: {
        type: DataTypes.STRING,
        allowNull: false
    },
    ClassName: {
        type: DataTypes.STRING,
        allowNull: false
    },
    lid: {
        type: DataTypes.INTEGER,
        allowNull: false
    }
})
async function init() {
    sequelize.sync({ force: true }).then(async () => {
        let logins = await Login.create(
            {
                zhanghao: 123456,
                password: 123456
            }
        )
        Class.bulkCreate(
            [{
                CName: "李达",
                Sex: "女",
                ClassName: "4班",
                lid: logins.id
            }]
        )
    }
    )
}
init();

const router = require("koa-router")()
const koa = require("koa")
const cors = require("koa-cors")
const body = require("koa-bodyparser")
router.get('/login', async (ctx) => {
    let res = ctx.query;
    zhanghao = parseInt(res.zhanghao)
    let list = await Login.findOne(
        {

            where: {

                zhanghao: zhanghao,
                password: res.password


            }
        }
    )
    if (list) {
        let cls = await Class.findOne(
            {
                where: {
                    lid: list.id
                }
            }
        )
        if (cls) {
            ctx.body = {
                code: 1000,
                msg: '登陆成功',
                data: list, cls
            }
        } else {
            let cls = await Class.create(
                {
                    CName: '未填写',
                    Sex: "未填写",
                    ClassName: "未填写",
                    lid: list.id
                }
            )
            ctx.body = {
                code: 1000,
                msg: '登陆成功',
                data: list, cls

            }

        }

    } else {
        ctx.body = {
            code: 900,
            msg: '登陆失败'
        }
    }
}).post('/login', async (ctx) => {
    let obj = ctx.request.body;
    console.log(obj);
    await Login.create(obj);
    ctx.body = (obj)
})
router.get('/class/:id', async (ctx) => {
    let cid = ctx.request.params.id
    let cls = await Class.findOne({
        where: {
            id: cid
        }
    })
    ctx.body = {
        code: 1000,
        msg: '成功',
        data: cls
    }
}).put('/class/:id', async (ctx) => {
    let cid = ctx.request.params.id
    let obj = ctx.request.body;
    await Class.update(obj,
        {
            where: {
                id: cid
            }
        }

    )
    ctx.body = {
        code: 1000,
        msg: '更新成功'
    }
}).delete('/class/:id', async (ctx) => {
    let id = ctx.request.params.id
    let cid = await Class.findOne(
        {
            where: {
                id: id
            }
        }
    )
    await Login.destroy({
        where: {
            id: cid.id
        }
    })
    await Class.destroy(
        {
            where: {
                id: id
            }
        }

    )

    ctx.body = {
        code: 1000,
        msg: '删除成功'
    }
})
let app = new koa();
app.use(cors()).use(body())
app.use(router.routes())
app.listen(8000);